package com.zhy.baselibrary.expand;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.baselibrary.R;
import com.zhy.baselibrary.shape.RoundLinearLayout;
import com.zhy.baselibrary.shape.RoundViewDelegate;


/**
 * toast管理器, 确保只有一个Toast对象
 */
@SuppressLint("ShowToast")
public class ToastMgr {
    private static Toast it;
    private static TextView tvToast;
    private static ImageView ivToast;
    private static RoundLinearLayout llToast;


    private ToastMgr() {
    }

    /**
     * 在程序初始化的时候调用, 只需调用一次
     */
    public static void init(Context _context) {
        View v = View.inflate(_context, R.layout.view_toast, null);
        tvToast = v.findViewById(R.id.tvToast);
        ivToast = v.findViewById(R.id.ivToast);
        llToast = v.findViewById(R.id.llToast);
        init(_context, v);
    }


    /**
     * 在程序初始化的时候调用, 只需调用一次
     */
    public static void init(Context _context, View view) {
        it = new Toast(_context);
        it.setView(view);
    }


    /**
     * 设置Toast背景
     */
    public static void setBackgroundView(View view) {
        checkInit();
        it.setView(view);
    }

    public static void show(CharSequence text, int duration) {
        checkInit();
//        it.setText(text);
        tvToast.setText(text);
        llToast.setGravity(Gravity.BOTTOM);
        tvToast.getPaint().setFakeBoldText(false);
        it.setDuration(duration);
        it.show();
    }

    public static void showCenter(CharSequence text, int duration) {
        checkInit();
        tvToast.setText(text);
        llToast.setGravity(Gravity.CENTER);
        it.setDuration(duration);
        it.show();
    }


    public static void show(int resid, int duration) {
        checkInit();
        it.setText(resid);
        it.setDuration(duration);
        it.show();
    }

    public static void show(CharSequence text) {
        llToast.getDelegate().setBackgroundColor(Color.parseColor("#41485D"));
        tvToast.setTextColor(Color.parseColor("#FFFFFF"));

        ivToast.setVisibility(View.GONE);
        show(text, Toast.LENGTH_SHORT);
    }

    public static void show(CharSequence text, String type) {
        RoundViewDelegate delegate = llToast.getDelegate();
        ivToast.setVisibility(View.VISIBLE);

        if ("success".equals(type)) {
            ivToast.setImageResource(R.mipmap.toast_success);
            delegate.setBackgroundColor(Color.parseColor("#D4F9E7"));
            tvToast.setTextColor(Color.parseColor("#1BC16E"));
        } else if ("fail".equals(type)) {
            ivToast.setImageResource(R.mipmap.toast_fail);
            delegate.setBackgroundColor(Color.parseColor("#FFE5E5"));
            tvToast.setTextColor(Color.parseColor("#D13945"));
        } else if ("warn".equals(type)) {
            ivToast.setImageResource(R.mipmap.toast_jinggao);
            delegate.setBackgroundColor(Color.parseColor("#FDF3DC"));
            tvToast.setTextColor(Color.parseColor("#FC9306"));
        }
        showCerter(text);
    }


    public static void showCerter(CharSequence text) {
        it.setGravity(Gravity.CENTER, it.getXOffset() / 2, it.getYOffset() / 2);
        showCenter(text, Toast.LENGTH_SHORT);
    }

    public static void showCenter(CharSequence text) {
        it.setGravity(Gravity.CENTER, it.getXOffset() / 2, it.getYOffset() / 2);
        showCenter(text, Toast.LENGTH_SHORT);
    }

    public static void show(int resId) {
        show(resId, Toast.LENGTH_SHORT);
    }

    private static void checkInit() {
        if (it == null) {
            throw new IllegalStateException("ToastMgr is not initialized, please call init once before you call this method");
        }

    }
}
